//#include <Python> What is this?  (I don't even...)
#include <iostream>
#include <cstdlib>
#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_image.h"
#include "gfx.h"
#include "SDL_collide.c"
#include "gameprog.h"
#include "audio.h"
#include "SDL_ttf.h"
#include "Objects/objects.cpp"
#include <ctime>
#if !defined(_WIN16) && !defined(_WIN32) && !defined(_WIN64)
        #include "gfx.cpp"
        #include "audio.cpp"
#endif

extern SDL_Surface *screen; 

//If you haven't already, please install the SDL-mixer packages!
//Including the development package

/*-------------------------------------------------------//
// GAMEPROGLIB.CPP                                       //
//    Contains general code for running the game.        //
// Process Functions:                                    //
//    main: !IMPORTANT! runs the game.                   //
//    createObject: pushes an object onto object vector  //
//-------------------------------------------------------*/
using namespace std;

int test();
int sdlpoll();

int main(int argc, char *argv[])
{
    if(test() != 0)
    {
      cout << endl;
      return 1;
    }
    else
    {
      return 0;
    }
}
int test()
{   
    unsigned short image1,image2,backgroundstack;
    short bgmusicstack, sfxstack;
    srand(time(NULL));  //set random seed
    output testoutput;
    sound testsound;
    testoutput.gfxinit();
    testoutput.setscreenmode(32,640,480);
    //Following code copypasted from gameproglib.cpp
    int noofsteps = 10000000;
    exampleObject testObject;
    exampleObject testObject2;
    testObject2.setVelocitySpDir(1,1);
    testObject.setVelocitySpDir(1,1);
    testObject2.jump(100,100);
    testObject.jump(testoutput.getparameter(1)/2,testoutput.getparameter(2)/2+100);
    testsound.setaudiomode(44100,2,4096);
    bgmusicstack = testsound.loadaudio("bayamesa.ogg");
    sfxstack = testsound.loadaudio("yell.wav");
    testsound.playaudio(bgmusicstack);
    image1 = testoutput.pushimage("fidel.gif");
    image2 = testoutput.pushimage("raul.gif");
    backgroundstack = testoutput.pushimage("havana.jpg");
    fprintf(stderr,"In Soviet Cuba, devving does YOU!.\n"); 
    testoutput.textinit();
    unsigned char font1 = testoutput.loadfont("tlpsmb.ttf",32);
    bool red1,red2,blue1,blue2;
    for ( int c = 0; c < noofsteps; c++ ) // c++ just for pun ;E
    {
        //if(testoutput.drawimage(image1,0,0,470,427,screen,0,0) == 1)
            //fprintf(stderr,"Could not blit!\n");
        //testObject.addQuickGravity();
        testoutput.cls();
        sdlpoll();
        testObject.executeStep();
        testObject2.executeStep();
        if(testoutput.testcollisionsimple(image1,image2))
        {
             testObject.jump(rand() % testoutput.getparameter(1),rand() % testoutput.getparameter(2));
             testObject2.jump(rand() % testoutput.getparameter(1),rand() % testoutput.getparameter(2));
             testsound.playaudio(sfxstack);
             //temp. disabled for annoyance reasons xD
        }
        for(int i=0;i<testsound.size();i++)
        {     
              if(testsound[i].playing == 1) testsound.soundloop(i);
        }
        if ((testObject.y < 0)||(testObject.y > testoutput.getparameter(2))||(testObject.x < 0)||(testObject.x > testoutput.getparameter(1)))  //If the object leaves the screen's y-axis...
        {
            testObject.jump(rand() % testoutput.getparameter(1),rand() % testoutput.getparameter(2));  //jump to a random location
            //testoutput.cls();
	    //testsound.playaudio(sfxstack);
        }
        if ((testObject2.y < 0)||(testObject2.y > testoutput.getparameter(2))||(testObject2.x < 0)||(testObject2.x > testoutput.getparameter(1)))  //If the object leaves the screen's y-axis...
        {
            testObject2.jump(rand() % testoutput.getparameter(1),rand() % testoutput.getparameter(2));  //jump to a random location
            //testoutput.cls();
	    //testsound.playaudio(sfxstack);
        }
        if ((testObject.x < 0)||(testObject.x > testoutput.getparameter(1)))  //If the object leaves the screen's x-axis...
         {
            testObject.jump(0,testObject.y);
            testoutput.cls();  //simply clear the screen, as it will wrap around
            //Later on, we will need to deal with x-axis wrapping in a more sophisticated manner ;-)
            //What's wrong with this? It's a small and efficient way of handling it. Who needs pretty?
        }
   	    //testoutput.updategfx(testObject.x,testoutput.getparameter(2) - testObject.y);
        if(testoutput.drawimage(backgroundstack,0,0,640,480,screen,0,0,255) == 1)
        {
            fprintf(stderr,"Could not blit background!\n");
        }
        if(testoutput.drawimage(image1,0,0,150,219,screen,testObject.x,testoutput.getparameter(2) - testObject.y,255) == 1)
        {
            fprintf(stderr,"Could not blit!\n");
        }
        if(testoutput.drawimage(image2,0,0,150,192,screen,testObject2.x,testoutput.getparameter(2) - testObject2.y,255) == 1)
        {
            fprintf(stderr,"Could not blit!\n");
        }
        
        red1 = time(NULL)%2;
        red2 = !red1;
        blue1= !red1;
        blue2 = red1;
        testoutput.printscr(font1,"THE SUPER CASTRO BROS",red1*255,0,blue1*255,255,0,0);
        testoutput.printscr(font1,"FIDEL AND RAUL",red2*255,0,blue2*255,255,0,450);
        testoutput.flip();
    }
    while(true)
    {
        sdlpoll();
    }
	return 1;
}

int sdlpoll()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
	    switch(event.type)
	    {
            case SDL_KEYDOWN:
            break;

            case SDL_KEYUP:
            if (event.key.keysym.sym == SDLK_ESCAPE) 
                exit(0);
            break;

	        case SDL_QUIT:
	        exit(0);

            default:
            break;
	    }
    }
    return 0;
}

